<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.services.autowait &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/alabaster.css" />
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.services.autowait</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Qiang Liu, Guannan Ma</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    auto wait related modules.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;wait_until_file_exist&#39;</span><span class="p">,</span> <span class="s1">&#39;wait_until_reg_str_exist&#39;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_not_exist&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_port_used&quot;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_used_ports&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_port_not_used&quot;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_exist&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_process_killed&quot;</span>
<span class="p">]</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup.shell</span> <span class="kn">import</span> <span class="n">oper</span>


<div class="viewcode-block" id="wait_until_file_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_file_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_file_exist</span><span class="p">(</span>
    <span class="n">dst_path</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">False</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait util the file exists or the function timeout</span>

<span class="sd">    :param dst_path:</span>
<span class="sd">        searching path</span>
<span class="sd">    :param file_name:</span>
<span class="sd">        filename, support *</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        max wating time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        check interval</span>
<span class="sd">    :param recursive:</span>
<span class="sd">        recursively search or not</span>
<span class="sd">    :return:</span>
<span class="sd">        True if found.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">contains_file</span><span class="p">(</span><span class="n">dst_path</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">recursive</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_reg_str_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_reg_str_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_reg_str_exist</span><span class="p">(</span>
    <span class="n">dst_file_path</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until any line in the file matches the \</span>
<span class="sd">    reg_str(regular expression string)</span>

<span class="sd">    :param dst_file_path:</span>
<span class="sd">        searching path</span>
<span class="sd">    :param reg_str:</span>
<span class="sd">        regular expression string</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        True if found</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">file_reader</span> <span class="o">=</span> <span class="n">FileReader</span><span class="p">(</span><span class="n">dst_file_path</span><span class="p">)</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">__check_reg_str_contain</span><span class="p">(</span><span class="n">file_reader</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_not_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_not_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_not_exist</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process does not exist anymore or the function timeouts</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process cwd</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout. 10 seconds by default</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval, 0.5 second by default</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the process disapper before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">process_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_proc_exist</span><span class="p">(</span><span class="n">pro_path</span><span class="p">,</span> <span class="n">pro_name</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_exist</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process exists</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        the specific process working path</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the process is found before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">process_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_proc_exist</span><span class="p">(</span><span class="n">pro_path</span><span class="p">,</span> <span class="n">pro_name</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_port_used"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_port_used">[docs]</a><span class="k">def</span> <span class="nf">wait_until_port_used</span><span class="p">(</span>
    <span class="n">port</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the port is used.  *Notice this function will invoke\</span>
<span class="sd">    a bash shell to execute command [netstat]!*</span>

<span class="sd">    :return:</span>
<span class="sd">        return True if the port is used</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_port_not_used"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_port_not_used">[docs]</a><span class="k">def</span> <span class="nf">wait_until_port_not_used</span><span class="p">(</span>
    <span class="n">port</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the port is free</span>

<span class="sd">    :return:</span>
<span class="sd">        return True if the port is free before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_used_ports"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_used_ports">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_used_ports</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">ports</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process has taken the ports before timeouts</span>

<span class="sd">    :return:</span>
<span class="sd">        True if all ports are used by the specific process.</span>
<span class="sd">        False, otherwise</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="n">used_port_num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">curr_port</span> <span class="ow">in</span> <span class="n">ports</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_process_used_port</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">curr_port</span><span class="p">):</span>
                <span class="n">used_port_num</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="n">used_port_num</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ports</span><span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_killed"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_killed">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_killed</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">ports</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the [process] does not exists and all [ports] are free</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process cwd</span>
<span class="sd">    :param ports:</span>
<span class="sd">        port list</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        True if all conditions meet.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="c1"># check process</span>
        <span class="k">if</span> <span class="kc">False</span> <span class="o">==</span> <span class="n">wait_until_process_not_exist</span><span class="p">(</span>
            <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span>
        <span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
            <span class="k">continue</span>
        <span class="c1"># check ports</span>
        <span class="n">not_used_port_num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">curr_port</span> <span class="ow">in</span> <span class="n">ports</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_process_used_port</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">curr_port</span><span class="p">):</span>
                <span class="n">not_used_port_num</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="n">not_used_port_num</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ports</span><span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<span class="k">def</span> <span class="nf">_wait_until_return</span><span class="p">(</span><span class="n">func</span><span class="p">,</span>
        <span class="n">boolean</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until function return [boolean]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">==</span> <span class="n">boolean</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">wait_return_true</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until func return true or max_wait_sec passes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_wait_until_return</span><span class="p">(</span>
        <span class="n">func</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">wait_return_false</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until func return False or max_wait_sec passes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_wait_until_return</span><span class="p">(</span>
        <span class="n">func</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">__check_reg_str_contain</span><span class="p">(</span><span class="n">file_reader</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    check if any line matches the reg_str</span>

<span class="sd">    :param file_reader:</span>
<span class="sd">        FileReade Object</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if found</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">file_content</span> <span class="o">=</span> <span class="n">file_reader</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">file_content</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">reg_str</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">class</span> <span class="nc">FileReader</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    this class is used to read file incremental</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span> <span class="o">=</span> <span class="n">file_path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_read_size</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        read from last position</span>

<span class="sd">        :param max_read_size:</span>
<span class="sd">            maximum reading length</span>
<span class="sd">        :return:</span>
<span class="sd">            content read</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># whether the file exist</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">):</span>
            <span class="k">return</span> <span class="s2">&quot;&quot;</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
            <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_END</span><span class="p">)</span>
            <span class="n">size</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">:</span>
                <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_SET</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="o">&gt;</span> <span class="p">(</span><span class="n">size</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">)):</span>
                    <span class="n">max_read_size</span> <span class="o">=</span> <span class="n">size</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">max_read_size</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>  <span class="c1"># may be a new file with the same name</span>
                <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_SET</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="o">&gt;</span> <span class="n">size</span><span class="p">):</span>
                    <span class="n">max_read_size</span> <span class="o">=</span> <span class="n">size</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">max_read_size</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>